Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

waddrmgr: fix lock order in importScriptAddress #958

Merged
merged 1 commit into from
Oct 21, 2024

Conversation

JssDWt
Copy link
Contributor

@JssDWt JssDWt commented Oct 7, 2024

First of all, the code between the scoped manager and the root manager needs a full rewrite. The lock orders are unmanageable. This PR fixes a potential deadlock (seen on Breez), but it doesn't fix the underlying issue here. The main problem is the locks between scoped manager and the root manager are taken in different orders. Sometimes root manager first, then scoped manager, sometimes the other way around.

For this PR the decision is to never lock the root manager inside a scoped manager lock. This PR only fixes a single occurence of this issue, there are more occurences elsewhere.

This PR has been in production for a swap server for Breez for a few months now and fixes our deadlock occurrences.

@guggero guggero self-requested a review October 8, 2024 05:54
Copy link
Collaborator

@guggero guggero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🎉

@@ -2324,6 +2321,20 @@ func (s *ScopedKeyManager) importScriptAddress(ns walletdb.ReadWriteBucket,
// Add the new managed address to the cache of recent addresses and
// return it.
s.addrs[addrKey(scriptIdent)] = managedAddr

if updateStartBlock {
// Now that the database has been updated, update the start block in
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: line is slightly over 80 characters if a tabulator size/width of 8 spaces is used.

// Now that the database has been updated, update the start block in
// memory too if needed. Ensure the manager lock goes outside the scoped
// manager lock.
s.mtx.Unlock()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're releasing the lock here, it's possible for a second/parallel call to importScriptAddress to come in and potentially progress to the if bs.Height < s.rootManager.syncState.startBlock.Height check before the below is updated, similar to how we could end up with duplicated addresses in lightningnetwork/lnd#8697. BUT, since we're just increasing the start block, the worst that could happen is that this write would happen twice. So IMO an acceptable solution until we do a full rewrite of this logic (which I agree is definitely necessary at some point).

@guggero guggero requested a review from yyforyongyu October 14, 2024 11:15
Copy link
Collaborator

@yyforyongyu yyforyongyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM❤️ Thanks for the fix!🍺

@guggero guggero merged commit 1a8e359 into btcsuite:master Oct 21, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants